package com.sys.service.impl;

import com.common.exception.ExceptionLog;
import com.common.inter.LogInter;
import com.sys.mapper.SysLogMapper;
import com.sys.model.SysLog;
import com.sys.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by dell on 2016/12/9.
 */
@Service
public class SysLogServiceImpl implements SysLogService,LogInter,ExceptionLog {

    @Autowired
    private SysLogMapper sysLogMapper;

    @Override
    @Async
    public int insert(SysLog sysLog) {
        return sysLogMapper.insert(sysLog);
    }

    @Override
    public int addStart(String id, String URL, String HTTP_METHOD,
                        String IP, String CLASS_METHOD, String ARGS, Date startTime,
                        String userId,String userName) {
        SysLog log=new SysLog();
        log.setLogId(id);
        log.setLogIp(IP);
        log.setLogHmethod(HTTP_METHOD);
        log.setLogUrl(URL);
        log.setLogArgs(ARGS);
        log.setLogCmethod(CLASS_METHOD);
        log.setLogTime(startTime);
        log.setErrorCode("0");
        log.setUserId(userId);
        log.setUserName(userName);
        return sysLogMapper.insertSelective(log);
    }

    @Override
    public int addEnd(String id, String RESPONSE, String SPEND) {
        SysLog log=sysLogMapper.selectByPrimaryKey(id);
        if(log==null)
        return 0;
        else
        {
            log.setLogResponse(RESPONSE);
            log.setLogDealtime(SPEND);
            return sysLogMapper.updateByPrimaryKeySelective(log);
        }
    }

    @Override
    public void log(String logId, String msg,String errorCode) {
        SysLog log=sysLogMapper.selectByPrimaryKey(logId);
        if(log==null)
            return;
        else
        {
            log.setLogResponse(msg);
            log.setErrorCode(errorCode);
            long dealTime=new Date().getTime()-log.getLogTime().getTime();
            log.setLogDealtime(""+dealTime);
            sysLogMapper.updateByPrimaryKeySelective(log);
        }
    }
}
